#include Arduino.h>

// Vamos a usar el GPIO19 por si el 18 sufrió algún daño en pruebas previas.
const int pwmPin = 19; 
const int pwmFreq = 50;        // 50 Hz -> período de 20 ms
const int pwmResolution = 10;  // 10 bits -> 0-1023

int numero = 0;

void setup() {
  delay(3000); // Reduje el delay a 3 segundos
  Serial.begin(115200);
  
  // Imprimir versión para confirmar qué estamos usando
  Serial.printf("Iniciando ESP32. Version del Core: %d\n", ESP_ARDUINO_VERSION_MAJOR);

  // NUEVA API PARA ESP32 CORE 3.x (Ya no usa ledcSetup)
  // Sintaxis: ledcAttach(pin, frecuencia, resolucion);
  bool success = ledcAttach(pwmPin, pwmFreq, pwmResolution);
  
  if(success) {
    Serial.println("PWM configurado correctamente en el pin " + String(pwmPin));
  } else {
    Serial.println("Error al configurar el PWM");
  }
}

void loop() {
  int duty = map(numero, 0, 10, 51, 972);
  
  // NUEVA API: Se escribe directamente al pin, ya no al canal
  ledcWrite(pwmPin, duty);

  Serial.print("Enviando numero: ");
  Serial.print(numero);
  Serial.print(" -> duty: ");
  Serial.println(duty);

  delay(2000); 
  numero++;
  if (numero > 10) numero = 0;
}